import Vue from 'vue'
import lodash from 'lodash'

let chy = {
  isEmpty: function (obj) {
    // 判断是否为null或undefined
    if (obj == null) return true
    // 判断是否为字符串
    if (typeof obj === 'string') return obj.trim().length === 0
    // 判断是否为数组
    if (Array.isArray(obj)) return obj.length === 0
    // 判断是否为对象
    if (typeof obj === 'object') return Object.keys(obj).length === 0
    // 其他情况返回false
    return false
  },
  /** lodash */
  lodash,
  /** 深拷贝 */
  cloneDeep: lodash.cloneDeep,
  /** 防抖 */
  debounce: lodash.debounce,
  /** 节流 */
  throttle: lodash.throttle,
  setCookie: function (name, value, perpetual) {
    const exdate = new Date()
    // exdate.setDate(exdate.getDate() + 365)
    exdate.setDate(exdate.getDate() + perpetual)
    document.cookie = name + '=' + value + ';expires=' + exdate.toGMTString()
    // 永久有效
    // document.cookie = name + '=' + value + ';expires=' + 'Fri, 31 Dec 9999 23:59:59 GMT'
  },

  /**
   * 获取cookies
   * @param {*} name 
   * @returns 
   */
  // name 字段名
  getCookie: function (name) {
    if (document.cookie.length > 0) {
      let start = document.cookie.indexOf(name + '=')
      if (start !== -1) {
        start = start + name.length + 1
        let end = document.cookie.indexOf(';', start)
        if (end === -1) end = document.cookie.length
        return unescape(document.cookie.substring(start, end))
      }
    }
    return ''
  },
  /**
 * 删除cookies
 * @param {*} name 
 */
  deleteCookie: function (name) {
    document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'
  },
  /**
   * 函数柯里化封装
   * @param {*} fn 
   * @param {*} args 
   * @returns 
   */
  curry: function (fn, args) {
    let length = fn.length
    let argArrs = args || []
    return function () {
      newArgs = argArrs.concat(Array.prototype.slice.call(arguments))
      if (newArgs.length < length) {
        return curry.call(this, fn, newArgs)
      } else {
        return fn.apply(this, newArgs)
      }
    }
  }
}

// 挂载全局
Vue.prototype.$chy = chy
window.$chy = chy
window._$chy = {}
